package org.jinyan.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jinyan.db.dao.IDBDao;
import org.jinyan.db.utils.ConnectionUtil;

public class DBDao implements IDBDao {

	public Connection connection = null;

	public DBDao() {
		init();
	}

	private void init() {
		System.out.println("数据连接初始化 start");
		if (connection == null) {
			connection = ConnectionUtil.getConn();
		}
		System.out.println("数据连接初始化 end");
	}

	@Override
	public boolean add(String sql, Object[] parms) throws SQLException {
		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		if (parms != null && parms.length > 0) {
			for (int i = 0; i < parms.length; i++) {
				preparedStatement.setString(i + 1, (String) parms[i]);
			}
		}
		preparedStatement.execute();
		boolean result = preparedStatement.getUpdateCount() > 0;
		ConnectionUtil.closePreparedStatement(preparedStatement);
		return result;
	}

	@Override
	public boolean update(String sql, Object[] parms) throws SQLException {

		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		if (parms != null && parms.length > 0) {
			for (int i = 0; i < parms.length; i++) {
				preparedStatement.setString(i + 1, (String) parms[i]);
			}
		}
		preparedStatement.execute();
		boolean result = preparedStatement.getUpdateCount() > 0;
		ConnectionUtil.closePreparedStatement(preparedStatement);
		return result;
	}

	@Override
	public boolean delete(String sql, Object[] parms) throws SQLException {

		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		if (parms != null && parms.length > 0) {
			for (int i = 0; i < parms.length; i++) {
				preparedStatement.setString(i + 1, (String) parms[i]);
			}
		}
		preparedStatement.execute();
		boolean result = preparedStatement.getUpdateCount() > 0;
		ConnectionUtil.closePreparedStatement(preparedStatement);
		return result;
	}

	@Override
	public Object selectByCondition(String sql, Object[] parms)
			throws SQLException {

		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		if (parms != null && parms.length > 0) {
			for (int i = 0; i < parms.length; i++) {
				preparedStatement.setString(i + 1, (String) parms[i]);
			}
		}
		ResultSet resultSet = preparedStatement.executeQuery();
		ResultSetMetaData metaData = resultSet.getMetaData();
		int count = metaData.getColumnCount();
		List result = new ArrayList();
		while (resultSet.next()) {
			Map map = new HashMap<String, Object>();
			for (int i = 0; i < count; i++) {
				String columnName = metaData.getColumnName(i + 1);
				String columnValue = resultSet.getString(columnName);
				map.put(columnName, columnValue);
			}
			result.add(map);
		}
		ConnectionUtil.closePreparedStatement(preparedStatement, resultSet);
		return result;
	}

	@Override
	public List<Object> selectList(String sql, Object[] parms)
			throws SQLException {
		System.out.println("DBdao Select List Sql= " + sql);

		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		if (parms != null && parms.length > 0) {
			for (int i = 0; i < parms.length; i++) {
				preparedStatement.setString(i + 1, (String) parms[i]);
			}
		}
		System.out.println("DBdao Select List parms= " + parms.toString());
		ResultSet resultSet = preparedStatement.executeQuery();
		List result = new ArrayList();
		if (resultSet != null) {
			ResultSetMetaData metaData = resultSet.getMetaData();
			int count = metaData.getColumnCount();
			while (resultSet.next()) {
				Map map = new HashMap<String, Object>();
				for (int i = 0; i < count; i++) {
					String columnName = metaData.getColumnName(i + 1);
					String columnValue = resultSet.getString(columnName);
					map.put(columnName, columnValue);
				}
				result.add(map);
			}
		}

		ConnectionUtil.closePreparedStatement(preparedStatement, resultSet);
		return result;
	}

	@Override
	public boolean add(String sql) throws SQLException {

		Statement statement = connection.createStatement();
		boolean result = false;
		statement.execute(sql);
		int temp = statement.getUpdateCount();
		result = temp > 0;
		ConnectionUtil.closeStatement(statement);
		return result;
	}

	@Override
	public boolean update(String sql) throws SQLException {
		if (connection == null) {
			connection = ConnectionUtil.getConn();
		}
		Statement statement = connection.createStatement();
		statement.execute(sql);
		int temp = statement.getUpdateCount();
		boolean result = temp > 0;

		ConnectionUtil.closeStatement(statement);

		return result;
	}

	@Override
	public boolean delete(String sql) throws SQLException {

		Statement statement = connection.createStatement();
		statement.execute(sql);
		boolean result = statement.getUpdateCount() > 0;
		ConnectionUtil.closeStatement(statement);
		return result;
	}

	@Override
	public Object select(String sql) throws SQLException {

		Statement statement = connection.createStatement();

		ResultSet resultSet = statement.executeQuery(sql);
		ResultSetMetaData metaData = resultSet.getMetaData();
		int count = metaData.getColumnCount();
		List result = new ArrayList();
		while (resultSet.next()) {
			Map map = new HashMap<String, Object>();
			for (int i = 0; i < count; i++) {
				String columnName = metaData.getColumnName(i + 1);
				String columnValue = resultSet.getString(columnName);
				map.put(columnName, columnValue);
			}
			result.add(map);

		}
		ConnectionUtil.closeStatement(statement, resultSet);
		return result;
	}

	@Override
	public List<Object> selectList(String sql) throws SQLException {
		Statement statement = connection.createStatement();

		ResultSet resultSet = statement.executeQuery(sql);
		ResultSetMetaData metaData = resultSet.getMetaData();
		int count = metaData.getColumnCount();
		List result = new ArrayList();
		while (resultSet.next()) {
			Map map = new HashMap<String, Object>();
			for (int i = 0; i < count; i++) {
				String columnName = metaData.getColumnName(i + 1);
				String columnValue = resultSet.getString(columnName);
				map.put(columnName, columnValue);
			}
			result.add(map);
		}
		ConnectionUtil.closeStatement(statement, resultSet);
		return result;
	}

}
